package com.yx.yinshanjinniu.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.BankListBean;
import com.yx.yinshanjinniu.bean.BindCardBean;
import com.yx.yinshanjinniu.bean.TxLiveTokenBean;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.CustomerUtil;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.LogUtil;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.utils.Utils;
import com.yx.yinshanjinniu.view.BindWithdrawCardDialog;
import com.yx.yinshanjinniu.view.MyEditText;

public class BindCardCertificateActivity extends BaseActivity implements View.OnClickListener {
    private MyEditText met_name, met_id_card_no, met_card_no, met_mobile;
    private RelativeLayout rl_bank;
    private TextView tv_bank, tv_custom_service;
    private Button bt_submit;
    private ImageView iv_custom_service;
    private BankListBean mBankBean;
    private String cert_no;
    private String cert_name;
    private String liveResult = "";
    private View v_step_02;
    private TextView tv_step_03;
    private BindWithdrawCardDialog bindWithdrawCardDialog;
    private TxLiveTokenBean txLiveData;
    private BindCardBean bindCardBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        String csString = "您在实名认证过程碰到任何问题请联系<font color=\"#1577FE\">在线客服</font>";
        Utils.setHtmlText(tv_custom_service, csString);

        UserInfoBean mAccountBean = DataUtils.getUserInfo(mContext);
        if (mAccountBean.auth_flag == 1) {
            cert_no = mAccountBean.certno;
            cert_name = mAccountBean.certname;

            if (!TextUtils.isEmpty(cert_no)) {
                met_id_card_no.setText(cert_no);
                met_id_card_no.setShowDeleteBtn(View.GONE);
                met_id_card_no.setEnable(false);
            }

            if (!TextUtils.isEmpty(cert_name)) {
                met_name.setText(cert_name);
                met_name.setShowDeleteBtn(View.GONE);
                met_name.setEnable(false);
            }

        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_bind_card_certificate;
    }

    @Override
    protected void findViews() {
        met_name = findViewById(R.id.met_name);
        met_id_card_no = findViewById(R.id.met_id_card_no);
        rl_bank = findViewById(R.id.rl_bank);
        tv_bank = findViewById(R.id.tv_bank);
        met_card_no = findViewById(R.id.met_card_no);
        met_mobile = findViewById(R.id.met_mobile);
        tv_custom_service = findViewById(R.id.tv_custom_service);
        bt_submit = findViewById(R.id.bt_submit);
        iv_custom_service = findViewById(R.id.iv_right);
        v_step_02 = findViewById(R.id.v_step_02);
        tv_step_03 = findViewById(R.id.tv_step_03);
    }

    @Override
    protected void setListeners() {
        bt_submit.setOnClickListener(this);
        rl_bank.setOnClickListener(this);
        tv_custom_service.setOnClickListener(this);
        iv_custom_service.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == bt_submit) {
            if (null == mBankBean) {
                ToastUtil.showToast(mContext, "请选择银行");
                return;
            }
            getTxLiveToken();
        } else if (v == rl_bank) {
            RouteUtil.goBankListActivity(this, RouteUtil.REQUEST_BANK_LIST, "1");
        } else if (v == tv_custom_service || v == iv_custom_service) {
            CustomerUtil.customerService(mContext);
        }
    }

    private void bindCard() {
        String mobile = met_mobile.getText().trim();
        String name = met_name.getText().trim();
        String idCard = met_id_card_no.getText().trim();
        String bankCard = met_card_no.getText().trim();
        loading();
        mAppAction.bindCard("1", bankCard, mobile, idCard, mBankBean.bank, name, new ActionCallbackListener<BindCardBean>() {
            @Override
            public void onSuccess(BindCardBean data) {
                cancelLoading();
                bindCardBean = data;
                //弹窗收验证码
                showBindCardConfirm();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });

    }

    private void showBindCardConfirm() {
        String mobile = met_mobile.getText().trim();
        String name = met_name.getText().trim();
        String idCard = met_id_card_no.getText().trim();
        String bankCard = met_card_no.getText().trim();
        loading();
        bindWithdrawCardDialog = new BindWithdrawCardDialog(mContext, false, Gravity.CENTER, bindCardBean.bind_card_id, txLiveData.orderNo, new BindWithdrawCardDialog.DialogClick() {
            @Override
            public void onClickCode() {
                mAppAction.bindCard("1", bankCard, mobile, idCard, mBankBean.bank, name, new ActionCallbackListener<BindCardBean>() {
                    @Override
                    public void onSuccess(BindCardBean data) {

                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        ToastUtil.showToast(mContext, message);
                    }
                });
            }
        });
        if (!bindWithdrawCardDialog.isShowing()) {
            bindWithdrawCardDialog.show();
        }
    }

    private void getTxLiveToken() {
        mAppAction.getTxLiveSdk(cert_no, cert_name, new ActionCallbackListener<TxLiveTokenBean>() {
            @Override
            public void onSuccess(TxLiveTokenBean data) {
                txLiveData = data;
                LogUtil.d("订单id", "返回" + data.orderNo);
                RouteUtil.toFaceVerifyActivity(BindCardCertificateActivity.this, cert_name, cert_no, data, RouteUtil.REQUEST_LIVE_REQUEST);
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getExtras() != null) {
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == RouteUtil.REQUEST_BANK_LIST) {
                    mBankBean = (BankListBean) data.getExtras().get(RouteUtil.BUNDLE_BANK_MODEL);
                    tv_bank.setText(mBankBean.name);
                }
                if (requestCode == RouteUtil.REQUEST_LIVE_REQUEST) {
                    liveResult = data.getExtras().getString(RouteUtil.BUNDLE_LIVE_RESULT);
                    boolean liveSuccess = data.getExtras().getBoolean(RouteUtil.BUNDLE_LIVE_IS_SUCCESS);
                    if (liveSuccess) {
                        v_step_02.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_blue_07));
                        tv_step_03.setTextColor(ContextCompat.getColor(mContext, R.color.color_blue_07));
                        tv_step_03.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.img_cert_step_03_check), null, null);
                        bindCard();
                    } else {
                        ToastUtil.showToast(mContext, "人脸识别失败，请重试");
                    }
                }
            }
        }
    }
}